package com.fs.ibox_h.dao.impl;

import com.fs.ibox_h.dao.withdrawalDao;
import com.fs.ibox_h.pojo.Withdrawa;
import com.fs.ibox_h.util.DateUtil;
import com.fs.ibox_h.util.JDBCUtil;
import com.fs.ibox_h.vo.Condition;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class withdrawalDaoImpl implements withdrawalDao {
    // 带条件分页查询
    @Override
    public List<Withdrawa> selectByCondition(int currentPage,int pageSize,Condition condition) {
        StringBuffer sql = new StringBuffer("SELECT id,username,w_money wMoney,money,w_type wType,charge,wid,wdate FROM tb_withdrawal WHERE 1=1");
        ArrayList<Object> list = new ArrayList<>();
        setValue(sql,condition,list);
        sql.append(" LIMIT ?,?");
        list.add((currentPage-1)*pageSize);
        list.add(pageSize);
        return JDBCUtil.executeQuery(sql.toString(), Withdrawa.class, list.toArray());
    }
    // 带条件查询总记录
    @Override
    public int count(Condition condition) {
        StringBuffer sql = new StringBuffer("SELECT COUNT(1) FROM tb_withdrawal WHERE 1=1");
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        ArrayList<Object> list = new ArrayList<>();
        // 拼接条件
        setValue(sql,condition,list);
        try {
            conn= JDBCUtil.getConnection();
            psmt = conn.prepareStatement(sql.toString());
            // 给？赋值
            for (int i = 0; i <list.size() ; i++) {
                psmt.setObject(i+1,list.get(i));
            }
            rs = psmt.executeQuery();
            rs.next();
            return rs.getInt(1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(rs,psmt,conn);
        }
        return 0;
    }

    @Override
    public int deleteById(int i) {
        String sql = "DELETE From tb_withdrawal Where id=?";
        return JDBCUtil.executeUpdate(sql,i);
    }

    @Override
    public int updateById(Integer id) {
        String sql = "UPDATE tb_withdrawal SET  w_type=1 WHERE id=?";
        return JDBCUtil.executeUpdate(sql,id);
    }

    private void setValue(StringBuffer sql, Condition condition, ArrayList<Object> list) {
        if (condition.getWid()!=null && !condition.getWid().isEmpty()){
            sql.append(" AND wid=?");
            list.add(condition.getWid());
        }else if (condition.getUsername()!=null && !condition.getUsername().isEmpty()){
            sql.append(" AND username=?");
            list.add(condition.getUsername());
        }else if (condition.getwType()!=null){
            sql.append(" AND w_type=?");
            list.add(condition.getwType());
        }else if (condition.getWdate()!=null){
            sql.append(" AND wdate>?");
            list.add(condition.getWdate());
        }
    }

   /* public static void main(String[] args) {
        Condition condition = new Condition();

        String date = "2022-11-17";
        try {
            condition.setWdate(DateUtil.toDate(date));
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<Withdrawa> withdrawas = new withdrawalDaoImpl().selectByCondition(1,4,condition);
        for (Withdrawa withdrawa : withdrawas) {
            System.out.println(withdrawa);
        }
    }*/

}
